package com.example.yupao.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;

/**
 * @Date 2024/6/19 10:35
 * @description: redie 序列化的配置
 * .
 * @Author LittleNight
 */


@Configuration // 打上注解
public class RedisConfig {

    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
        // 创建RedisTemplate 对象
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();

        // 设置连接工厂
        redisTemplate.setConnectionFactory(connectionFactory);

        // 设置 key 的序列化. 采用 string 序列化
        redisTemplate.setKeySerializer(RedisSerializer.string());

        // 设置序列化工具
        GenericJackson2JsonRedisSerializer jsonRedisSerializer =
                                                new GenericJackson2JsonRedisSerializer();
        // value 采用 json 序列化
        redisTemplate.setValueSerializer(jsonRedisSerializer);
        return redisTemplate;
    }
}
